package com.bears.god.sup.servlet.toAction;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.bears.god.sup.model.Bill;
import com.bears.god.sup.model.Vendor;
import com.bears.god.sup.service.IBillService;
import com.bears.god.sup.service.IVendorService;
import com.bears.god.sup.service.ServiceDAOFactory;

@WebServlet("/querybill.do")
public class ToQueryBillServlet extends HttpServlet {

	private static final long serialVersionUID = 000123L;

	public static final String METHOD_QUERY = "0";
	public static final String METHOD_FINISH = "1";
	public static final String METHOD_BILL = "2";
	public static final String MATHOD_UPDATE = "3";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");

		if (method != null) {
			switch (method) {
			case ToQueryBillServlet.METHOD_QUERY:
				query(req, resp);
				break;
			case ToQueryBillServlet.METHOD_FINISH:
				finish(req, resp);
				break;
			case ToQueryBillServlet.METHOD_BILL:
				Bill(req, resp);
				break;
			case ToQueryBillServlet.MATHOD_UPDATE:
				Update(req,resp);
				break;
			default:
				break;
			}
		}
	}


	private void Bill(HttpServletRequest req, HttpServletResponse resp) {
		List<Vendor> vendors;
		try {
			vendors = com.bears.god.sup.service.ServiceDAOFactory.getVendorDAO().findAll();
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
			com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();

			for (Vendor vendor : vendors) {
				com.alibaba.fastjson.JSONObject json1 = (com.alibaba.fastjson.JSONObject) JSON.toJSON(vendor);
				array.add(json1);
			}

			json.put("vendors", array);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void query(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getRequestDispatcher("WEB-INF/view/cashier/queryBill.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void finish(HttpServletRequest req, HttpServletResponse resp) {
		String name = req.getParameter("vendorName");
		try {
			List<Bill> billname = ServiceDAOFactory.getBillDAO().findByName(name);
			
			req.getSession().setAttribute("billname", billname);
			req.getRequestDispatcher("WEB-INF/view/cashier/billInfo.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void Update(HttpServletRequest req, HttpServletResponse resp) {
		Bill bill = new Bill();
		try{
			
			int id = Integer.parseInt(req.getParameter("id"));
			Bill bills =  ServiceDAOFactory.getBillDAO().findById(id);
			req.getSession().setAttribute("bills", bills);
			bill.setId(id);
			bill.setBillPay("是");
			String paidFlag = req.getParameter("paidFlag");
			if(paidFlag==null){
				bill.setPaidFlag("現金支付");
			}else{
				bill.setPaidFlag(paidFlag);
			}
			System.out.println(req.getParameter("paidFlag"));
			
			
			IBillService service = ServiceDAOFactory.getBillDAO();
			service.update(bill);
			req.setAttribute("bill", bill);

			String name = req.getParameter("vendorName");
			List<Bill> billname = ServiceDAOFactory.getBillDAO().findByName(name);
			req.getSession().setAttribute("billname", billname);
			req.getRequestDispatcher("WEB-INF/view/cashier/billInfo.jsp").forward(req, resp);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
